

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>XGBoost Exporter &mdash; Nyoka 4.2.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript">
          var DOCUMENTATION_OPTIONS = {
              URL_ROOT:'../',
              VERSION:'4.2.0',
              LANGUAGE:'None',
              COLLAPSE_INDEX:false,
              FILE_SUFFIX:'.html',
              HAS_SOURCE:  true,
              SOURCELINK_SUFFIX: '.txt'
          };
      </script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home"> Nyoka
          

          
          </a>

          
            
            
              <div class="version">
                4.2
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="../statsmodels_to_pmml.html">Statsmodels Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../keras_model_to_pmml.html">Keras Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../retinanet.html">RetinaNet Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../lgb_to_pmml.html">LightGBM Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../pre_process.html">Pre-Processing Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../skl_to_pmml.html">Scikit-Learn Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../xgboost_to_pmml.html">XGBoost Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../exponential_smoothing.html">ExponentialSmoothing Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../preprocess_nyoka.html">Nyoka's Pre-Processing Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../enums.html">Enums Module</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Nyoka</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">Module code</a> &raquo;</li>
        
      <li>XGBoost Exporter</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for XGBoost Exporter</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">absolute_import</span>

<span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">os</span>
<span class="n">BASE_DIR</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">BASE_DIR</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">PMML44</span> <span class="k">as</span> <span class="nn">pml</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">skl_to_pmml</span> <span class="k">as</span> <span class="nn">sklToPmml</span>
<span class="kn">import</span> <span class="nn">pre_process</span> <span class="k">as</span> <span class="nn">pp</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>
<span class="kn">from</span> <span class="nn">enums</span> <span class="k">import</span> <span class="o">*</span>


<div class="viewcode-block" id="xgboost_to_pmml"><span class="k">def</span> <span class="nf">xgboost_to_pmml</span><span class="p">(</span><span class="n">pipeline</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">pmml_f_name</span><span class="o">=</span><span class="s1">&#39;from_xgboost.pmml&#39;</span><span class="p">,</span><span class="n">model_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">description</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Exports xgboost model object into pmml</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    pipeline :</span>
<span class="sd">        Contains an instance of Pipeline with preprocessing and final estimator</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the target column.</span>
<span class="sd">    pmml_f_name : String</span>
<span class="sd">        Name of the pmml file. (Default=&#39;from_xgboost.pmml&#39;)</span>
<span class="sd">    model_name : string (optional)</span>
<span class="sd">        Name of the model</span>
<span class="sd">    description : string (optional)</span>
<span class="sd">        Description for the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Generates the PMML object and exports it to `pmml_f_name`</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">pipeline</span><span class="o">.</span><span class="n">steps</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Exporter expects pipeleine_instance and not an estimator_instance&quot;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">col_names</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;ndarray&quot;</span><span class="p">:</span>
            <span class="n">col_names</span> <span class="o">=</span> <span class="n">col_names</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="n">ppln_sans_predictor</span> <span class="o">=</span> <span class="n">pipeline</span><span class="o">.</span><span class="n">steps</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">trfm_dict_kwargs</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
        <span class="n">derived_col_names</span> <span class="o">=</span> <span class="n">col_names</span>
        <span class="n">categoric_values</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">()</span>
        <span class="n">mining_imp_val</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">ppln_sans_predictor</span><span class="p">:</span>
            <span class="n">pml_pp</span> <span class="o">=</span> <span class="n">pp</span><span class="o">.</span><span class="n">get_preprocess_val</span><span class="p">(</span><span class="n">ppln_sans_predictor</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">model</span><span class="p">)</span>
            <span class="n">trfm_dict_kwargs</span><span class="p">[</span><span class="s1">&#39;TransformationDictionary&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pml_pp</span><span class="p">[</span><span class="s1">&#39;trfm_dict&#39;</span><span class="p">]</span>
            <span class="n">derived_col_names</span> <span class="o">=</span> <span class="n">pml_pp</span><span class="p">[</span><span class="s1">&#39;derived_col_names&#39;</span><span class="p">]</span>
            <span class="n">col_names</span> <span class="o">=</span> <span class="n">pml_pp</span><span class="p">[</span><span class="s1">&#39;preprocessed_col_names&#39;</span><span class="p">]</span>
            <span class="n">categoric_values</span> <span class="o">=</span> <span class="n">pml_pp</span><span class="p">[</span><span class="s1">&#39;categorical_feat_values&#39;</span><span class="p">]</span>
            <span class="n">mining_imp_val</span> <span class="o">=</span> <span class="n">pml_pp</span><span class="p">[</span><span class="s1">&#39;mining_imp_values&#39;</span><span class="p">]</span>
        <span class="n">PMML_kwargs</span> <span class="o">=</span> <span class="n">get_PMML_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                      <span class="n">derived_col_names</span><span class="p">,</span>
                                      <span class="n">col_names</span><span class="p">,</span>
                                      <span class="n">target_name</span><span class="p">,</span>
                                      <span class="n">mining_imp_val</span><span class="p">,</span>
                                      <span class="n">categoric_values</span><span class="p">,</span>
                                      <span class="n">model_name</span><span class="p">)</span>
        <span class="n">pmml</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">PMML</span><span class="p">(</span>
            <span class="n">version</span><span class="o">=</span><span class="n">PMML_SCHEMA</span><span class="o">.</span><span class="n">VERSION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
            <span class="n">Header</span><span class="o">=</span><span class="n">sklToPmml</span><span class="o">.</span><span class="n">get_header</span><span class="p">(</span><span class="n">description</span><span class="p">),</span>
            <span class="n">DataDictionary</span><span class="o">=</span><span class="n">sklToPmml</span><span class="o">.</span><span class="n">get_data_dictionary</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">),</span>
            <span class="o">**</span><span class="n">trfm_dict_kwargs</span><span class="p">,</span>
            <span class="o">**</span><span class="n">PMML_kwargs</span>
        <span class="p">)</span>
        <span class="n">pmml</span><span class="o">.</span><span class="n">export</span><span class="p">(</span><span class="n">outfile</span><span class="o">=</span><span class="nb">open</span><span class="p">(</span><span class="n">pmml_f_name</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">),</span> <span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span></div>

<div class="viewcode-block" id="get_PMML_kwargs"><span class="k">def</span> <span class="nf">get_PMML_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns all the pmml elements.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        Contains XGBoost model object.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the target column .</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    algo_kwargs : { dictionary element}</span>
<span class="sd">        Get the PMML model argument based on XGBoost model object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;MiningModel&#39;</span><span class="p">:</span> <span class="n">get_ensemble_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                      <span class="n">derived_col_names</span><span class="p">,</span>
                                                      <span class="n">col_names</span><span class="p">,</span>
                                                      <span class="n">target_name</span><span class="p">,</span>
                                                      <span class="n">mining_imp_val</span><span class="p">,</span>
                                                      <span class="n">categoric_values</span><span class="p">,</span>
                                                      <span class="n">model_name</span><span class="p">)}</span>
    <span class="k">return</span> <span class="n">algo_kwargs</span></div>

<div class="viewcode-block" id="get_ensemble_models"><span class="k">def</span> <span class="nf">get_ensemble_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Mining Model element of the model</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        Contains Xgboost model object.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value.</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    mining_models :</span>
<span class="sd">        Returns Nyoka&#39;s MiningModel object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="n">sklToPmml</span><span class="o">.</span><span class="n">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">)</span>
    <span class="k">if</span> <span class="s1">&#39;XGBRegressor&#39;</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span>
        <span class="n">model_kwargs</span><span class="p">[</span><span class="s1">&#39;Targets&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">sklToPmml</span><span class="o">.</span><span class="n">get_targets</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">target_name</span><span class="p">)</span>
    <span class="n">mining_models</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">mining_models</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningModel</span><span class="p">(</span>
        <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="s2">&quot;XGBoostModel&quot;</span><span class="p">,</span>
        <span class="n">Segmentation</span><span class="o">=</span><span class="n">get_outer_segmentation</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">),</span>
        <span class="o">**</span><span class="n">model_kwargs</span>
    <span class="p">))</span>
    <span class="k">return</span> <span class="n">mining_models</span></div>



<div class="viewcode-block" id="get_outer_segmentation"><span class="k">def</span> <span class="nf">get_outer_segmentation</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Segmentation element of the model.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        Contains Xgboost model object.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    segmentation :</span>
<span class="sd">        Returns Nyoka&#39;s Segmentation object</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="s1">&#39;XGBRegressor&#39;</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span>
        <span class="n">segmentation</span><span class="o">=</span><span class="n">get_segments</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">segmentation</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segmentation</span><span class="p">(</span>
            <span class="n">multipleModelMethod</span><span class="o">=</span><span class="n">get_multiple_model_method</span><span class="p">(</span><span class="n">model</span><span class="p">),</span>
            <span class="n">Segment</span><span class="o">=</span><span class="n">get_segments</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">)</span>
        <span class="p">)</span>
    <span class="k">return</span> <span class="n">segmentation</span></div>

<div class="viewcode-block" id="get_segments"><span class="k">def</span> <span class="nf">get_segments</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Segment element of the model.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        Contains Xgboost model object.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    segment :</span>
<span class="sd">        Nyoka&#39;s Segment object</span>

<span class="sd">   &quot;&quot;&quot;</span>
    <span class="n">segments</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">if</span> <span class="s1">&#39;XGBClassifier&#39;</span>  <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span>
        <span class="n">segments</span><span class="o">=</span><span class="n">get_segments_for_xgbc</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">)</span>
    <span class="k">elif</span> <span class="s1">&#39;XGBRegressor&#39;</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span>
        <span class="n">segments</span><span class="o">=</span><span class="n">get_segments_for_xgbr</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">segments</span></div>

<div class="viewcode-block" id="get_segments_for_xgbr"><span class="k">def</span> <span class="nf">get_segments_for_xgbr</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">feature_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categorical_values</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns all the Segments element of the model</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        Contains Xgboost model object.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    feature_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : List</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    segment :</span>
<span class="sd">        Nyoka&#39;s Segment object</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">segments</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">get_nodes_in_json_format</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">n_estimators</span><span class="p">):</span>
        <span class="n">get_nodes_in_json_format</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_Booster</span><span class="o">.</span><span class="n">get_dump</span><span class="p">(</span><span class="n">dump_format</span><span class="o">=</span><span class="s1">&#39;json&#39;</span><span class="p">)[</span><span class="n">i</span><span class="p">]))</span>
    <span class="n">segmentation</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segmentation</span><span class="p">(</span><span class="n">multipleModelMethod</span><span class="o">=</span><span class="n">MULTIPLE_MODEL_METHOD</span><span class="o">.</span><span class="n">SUM</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                    <span class="n">Segment</span><span class="o">=</span><span class="n">generate_Segments_Equal_To_Estimators</span><span class="p">(</span><span class="n">get_nodes_in_json_format</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span>
                                                                                  <span class="n">feature_names</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">segmentation</span></div>


<div class="viewcode-block" id="mining_Field_For_First_Segment"><span class="k">def</span> <span class="nf">mining_Field_For_First_Segment</span><span class="p">(</span><span class="n">feature_names</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Mining Schema of the First Segment.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    feature_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    </span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    mining_schema_for_1st_segment :</span>
<span class="sd">        Nyoka&#39;s MiningSchema object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">mining_fields_1st_segment</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">feature_names</span><span class="p">:</span>
        <span class="n">mining_fields_1st_segment</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">))</span>
    <span class="n">mining_schema_for_1st_segment</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">MiningSchema</span><span class="p">(</span><span class="n">MiningField</span><span class="o">=</span><span class="n">mining_fields_1st_segment</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">mining_schema_for_1st_segment</span></div>

<div class="viewcode-block" id="replace_name_with_derivedColumnNames"><span class="k">def</span> <span class="nf">replace_name_with_derivedColumnNames</span><span class="p">(</span><span class="n">original_name</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It replace the default names with the names of the attributes.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    original_name : List</span>
<span class="sd">        The name of the node retrieve from model</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">    The name of the derived attributes.</span>
<span class="sd">    </span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    col_name :</span>
<span class="sd">        Returns the derived column name/original column name.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">new</span> <span class="o">=</span> <span class="nb">str</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">original_name</span><span class="p">,</span> <span class="s1">&#39;f&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">new</span><span class="o">.</span><span class="n">isdigit</span><span class="p">():</span>
        <span class="n">col_name</span> <span class="o">=</span> <span class="n">derived_col_names</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">new</span><span class="p">)]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">col_name</span> <span class="o">=</span> <span class="n">original_name</span>
    <span class="k">return</span> <span class="n">col_name</span></div>


<div class="viewcode-block" id="create_node"><span class="k">def</span> <span class="nf">create_node</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">main_node</span><span class="p">,</span><span class="n">derived_col_names</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It creates nodes.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    obj : Json</span>
<span class="sd">        Contains nodes in json format.</span>
<span class="sd">    main_node :</span>
<span class="sd">        Contains node build with Nyoka class.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">create_left_node</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="n">derived_col_names</span><span class="p">):</span>
        <span class="n">nd</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Node</span><span class="p">()</span>
        <span class="n">nd</span><span class="o">.</span><span class="n">set_SimplePredicate</span><span class="p">(</span>
            <span class="n">pml</span><span class="o">.</span><span class="n">SimplePredicate</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="n">replace_name_with_derivedColumnNames</span><span class="p">(</span><span class="n">obj</span><span class="p">[</span><span class="s1">&#39;split&#39;</span><span class="p">],</span> <span class="n">derived_col_names</span><span class="p">),</span>\
                 <span class="n">operator</span><span class="o">=</span><span class="n">SIMPLE_PREDICATE_OPERATOR</span><span class="o">.</span><span class="n">LESS_THAN</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">obj</span><span class="p">[</span><span class="s1">&#39;split_condition&#39;</span><span class="p">])))</span>
        <span class="n">create_node</span><span class="p">(</span><span class="n">obj</span><span class="p">[</span><span class="s1">&#39;children&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">nd</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">nd</span>

    <span class="k">def</span> <span class="nf">create_right_node</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="n">derived_col_names</span><span class="p">):</span>
        <span class="n">nd</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Node</span><span class="p">()</span>
        <span class="n">nd</span><span class="o">.</span><span class="n">set_SimplePredicate</span><span class="p">(</span>
            <span class="n">pml</span><span class="o">.</span><span class="n">SimplePredicate</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="n">replace_name_with_derivedColumnNames</span><span class="p">(</span><span class="n">obj</span><span class="p">[</span><span class="s1">&#39;split&#39;</span><span class="p">],</span> <span class="n">derived_col_names</span><span class="p">),</span>\
                 <span class="n">operator</span><span class="o">=</span><span class="n">SIMPLE_PREDICATE_OPERATOR</span><span class="o">.</span><span class="n">GREATER_OR_EQUAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">obj</span><span class="p">[</span><span class="s1">&#39;split_condition&#39;</span><span class="p">])))</span>
        <span class="n">create_node</span><span class="p">(</span><span class="n">obj</span><span class="p">[</span><span class="s1">&#39;children&#39;</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">nd</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">nd</span>

    <span class="k">if</span> <span class="s1">&#39;split&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">obj</span><span class="p">:</span>
        <span class="n">main_node</span><span class="o">.</span><span class="n">set_score</span><span class="p">(</span><span class="n">obj</span><span class="p">[</span><span class="s1">&#39;leaf&#39;</span><span class="p">])</span>
    <span class="k">else</span><span class="p">:</span>

        <span class="n">main_node</span><span class="o">.</span><span class="n">add_Node</span><span class="p">(</span><span class="n">create_left_node</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="n">derived_col_names</span><span class="p">))</span>
        <span class="n">main_node</span><span class="o">.</span><span class="n">add_Node</span><span class="p">(</span><span class="n">create_right_node</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="n">derived_col_names</span><span class="p">))</span></div>


<div class="viewcode-block" id="generate_Segments_Equal_To_Estimators"><span class="k">def</span> <span class="nf">generate_Segments_Equal_To_Estimators</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns number of Segments equal to the estimator of the model.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    val : List</span>
<span class="sd">        Contains a list of well structured node for binary classification/inner segments for multi-class classification</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    </span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    segments_equal_to_estimators:</span>
<span class="sd">        Nyoka&#39;s Segment object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">segments_equal_to_estimators</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">val</span><span class="p">)):</span>
        <span class="n">main_node</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Node</span><span class="p">(</span><span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">())</span>
        <span class="n">m_flds</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">mining_field_for_innner_segments</span> <span class="o">=</span> <span class="n">col_names</span>
        <span class="n">create_node</span><span class="p">(</span><span class="n">val</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">main_node</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">mining_field_for_innner_segments</span><span class="p">:</span>
            <span class="n">m_flds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">))</span>

        <span class="n">segments_equal_to_estimators</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">pml</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">(),</span>
                                                         <span class="n">TreeModel</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">TreeModel</span><span class="p">(</span><span class="n">functionName</span><span class="o">=</span><span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                                         <span class="n">modelName</span><span class="o">=</span><span class="s2">&quot;DecisionTreeModel&quot;</span><span class="p">,</span>
                                                                                 <span class="n">missingValueStrategy</span><span class="o">=</span><span class="s2">&quot;none&quot;</span><span class="p">,</span>
                                                                                 <span class="n">noTrueChildStrategy</span><span class="o">=</span><span class="s2">&quot;returnLastPrediction&quot;</span><span class="p">,</span>
                                                                                 <span class="n">splitCharacteristic</span><span class="o">=</span><span class="n">TREE_SPLIT_CHARACTERISTIC</span><span class="o">.</span><span class="n">MULTI</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                                                                 <span class="n">Node</span><span class="o">=</span><span class="n">main_node</span><span class="p">,</span>
                                                                                 <span class="n">MiningSchema</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningSchema</span><span class="p">(</span>
                                                                                     <span class="n">MiningField</span><span class="o">=</span><span class="n">m_flds</span><span class="p">)))))</span>

    <span class="k">return</span> <span class="n">segments_equal_to_estimators</span></div>

<div class="viewcode-block" id="add_segmentation"><span class="k">def</span> <span class="nf">add_segmentation</span><span class="p">(</span><span class="n">model</span><span class="p">,</span><span class="n">segments_equal_to_estimators</span><span class="p">,</span><span class="n">mining_schema_for_1st_segment</span><span class="p">,</span><span class="n">out</span><span class="p">,</span><span class="nb">id</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns segmentation for a mining model</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">       Contains Xgboost model object.</span>
<span class="sd">    segments_equal_to_estimators : List</span>
<span class="sd">        Contains List Segements equals to the number of the estimators of the model.</span>
<span class="sd">    mining_schema_for_1st_segment :</span>
<span class="sd">        Contains Mining Schema for the First Segment</span>
<span class="sd">    out :</span>
<span class="sd">        Contains the Output element</span>
<span class="sd">    id : Integer</span>
<span class="sd">        Index of the Segements</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    segments_equal_to_estimators:</span>
<span class="sd">         Returns Nyoka&#39;s Segment object</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">segmentation</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segmentation</span><span class="p">(</span><span class="n">multipleModelMethod</span><span class="o">=</span><span class="n">MULTIPLE_MODEL_METHOD</span><span class="o">.</span><span class="n">SUM</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">Segment</span><span class="o">=</span><span class="n">segments_equal_to_estimators</span><span class="p">)</span>
    <span class="n">mining_model</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">MiningModel</span><span class="p">(</span><span class="n">functionName</span><span class="o">=</span><span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">modelName</span><span class="o">=</span><span class="s2">&quot;MiningModel&quot;</span><span class="p">,</span> <span class="n">MiningSchema</span><span class="o">=</span><span class="n">mining_schema_for_1st_segment</span><span class="p">,</span>
                                         <span class="n">Output</span><span class="o">=</span><span class="n">out</span><span class="p">,</span> <span class="n">Segmentation</span><span class="o">=</span><span class="n">segmentation</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">n_classes_</span><span class="o">==</span><span class="mi">2</span><span class="p">:</span>
        <span class="n">First_segment</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">(),</span> <span class="nb">id</span><span class="o">=</span><span class="nb">id</span><span class="p">,</span> <span class="n">MiningModel</span><span class="o">=</span><span class="n">mining_model</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">First_segment</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">segments_equal_to_class</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">(),</span> <span class="nb">id</span><span class="o">=</span><span class="nb">id</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">MiningModel</span><span class="o">=</span><span class="n">mining_model</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">segments_equal_to_class</span></div>




<div class="viewcode-block" id="get_segments_for_xgbc"><span class="k">def</span> <span class="nf">get_segments_for_xgbc</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">feature_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns all the segments of the Xgboost classifier.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        Contains Xgboost model object.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    feature_names: List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    regrs_models :</span>
<span class="sd">        Returns Nyoka&#39;s Segment object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">segments</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">n_classes_</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">get_nodes_in_json_format</span><span class="o">=</span><span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">n_estimators</span><span class="p">):</span>
            <span class="n">get_nodes_in_json_format</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_Booster</span><span class="o">.</span><span class="n">get_dump</span><span class="p">(</span><span class="n">dump_format</span><span class="o">=</span><span class="s1">&#39;json&#39;</span><span class="p">)[</span><span class="n">i</span><span class="p">]))</span>
        <span class="n">mining_schema_for_1st_segment</span> <span class="o">=</span> <span class="n">mining_Field_For_First_Segment</span><span class="p">(</span><span class="n">feature_names</span><span class="p">)</span>
        <span class="n">outputField</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="n">outputField</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;xgbValue&quot;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">FLOAT</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                           <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">PREDICTED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">isFinalResult</span><span class="o">=</span><span class="s2">&quot;true&quot;</span><span class="p">))</span>
        <span class="n">out</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Output</span><span class="p">(</span><span class="n">OutputField</span><span class="o">=</span><span class="n">outputField</span><span class="p">)</span>
        <span class="n">oField</span><span class="o">=</span><span class="nb">list</span><span class="p">()</span>
        <span class="n">oField</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;xgbValue&#39;</span><span class="p">)</span>
        <span class="n">segments_equal_to_estimators</span> <span class="o">=</span> <span class="n">generate_Segments_Equal_To_Estimators</span><span class="p">(</span><span class="n">get_nodes_in_json_format</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span>
                                                                             <span class="n">feature_names</span><span class="p">)</span>
        <span class="n">First_segment</span> <span class="o">=</span> <span class="n">add_segmentation</span><span class="p">(</span><span class="n">model</span><span class="p">,</span><span class="n">segments_equal_to_estimators</span><span class="p">,</span> <span class="n">mining_schema_for_1st_segment</span><span class="p">,</span> <span class="n">out</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">reg_model</span><span class="o">=</span><span class="n">sklToPmml</span><span class="o">.</span><span class="n">get_regrs_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">oField</span><span class="p">,</span> <span class="n">oField</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span><span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">reg_model</span><span class="o">.</span><span class="n">normalizationMethod</span><span class="o">=</span><span class="n">REGRESSION_NORMALIZATION_METHOD</span><span class="o">.</span><span class="n">LOGISTIC</span><span class="o">.</span><span class="n">value</span>
        <span class="n">last_segment</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">(),</span> <span class="nb">id</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
                                   <span class="n">RegressionModel</span><span class="o">=</span><span class="n">reg_model</span><span class="p">)</span>
        <span class="n">segments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">First_segment</span><span class="p">)</span>

        <span class="n">segments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">last_segment</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>

        <span class="n">get_nodes_in_json_format</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">n_estimators</span> <span class="o">*</span> <span class="n">model</span><span class="o">.</span><span class="n">n_classes_</span><span class="p">):</span>
            <span class="n">get_nodes_in_json_format</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_Booster</span><span class="o">.</span><span class="n">get_dump</span><span class="p">(</span><span class="n">dump_format</span><span class="o">=</span><span class="s1">&#39;json&#39;</span><span class="p">)[</span><span class="n">i</span><span class="p">]))</span>
        <span class="n">oField</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">n_classes_</span><span class="p">):</span>
            <span class="n">inner_segment</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">in_seg</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">get_nodes_in_json_format</span><span class="p">),</span> <span class="n">model</span><span class="o">.</span><span class="n">n_classes_</span><span class="p">):</span>
                <span class="n">inner_segment</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">get_nodes_in_json_format</span><span class="p">[</span><span class="n">in_seg</span><span class="p">])</span>
            <span class="n">mining_schema_for_1st_segment</span> <span class="o">=</span> <span class="n">mining_Field_For_First_Segment</span><span class="p">(</span><span class="n">feature_names</span><span class="p">)</span>
            <span class="n">outputField</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
            <span class="n">outputField</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;xgbValue(&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">index</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                      <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">PREDICTED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">FLOAT</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">isFinalResult</span><span class="o">=</span><span class="s2">&quot;true&quot;</span><span class="p">))</span>
            <span class="n">out</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Output</span><span class="p">(</span><span class="n">OutputField</span><span class="o">=</span><span class="n">outputField</span><span class="p">)</span>

            <span class="n">oField</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;xgbValue(&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">index</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span><span class="p">)</span>
            <span class="n">segments_equal_to_estimators</span> <span class="o">=</span> <span class="n">generate_Segments_Equal_To_Estimators</span><span class="p">(</span><span class="n">inner_segment</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span>
                                                                                 <span class="n">feature_names</span><span class="p">)</span>
            <span class="n">segments_equal_to_class</span> <span class="o">=</span> <span class="n">add_segmentation</span><span class="p">(</span><span class="n">model</span><span class="p">,</span><span class="n">segments_equal_to_estimators</span><span class="p">,</span>
                                                       <span class="n">mining_schema_for_1st_segment</span><span class="p">,</span> <span class="n">out</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span>
            <span class="n">segments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">segments_equal_to_class</span><span class="p">)</span>
        <span class="n">reg_model</span><span class="o">=</span><span class="n">sklToPmml</span><span class="o">.</span><span class="n">get_regrs_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span><span class="n">oField</span><span class="p">,</span><span class="n">oField</span><span class="p">,</span><span class="n">target_name</span><span class="p">,</span><span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">reg_model</span><span class="o">.</span><span class="n">normalizationMethod</span><span class="o">=</span><span class="n">REGRESSION_NORMALIZATION_METHOD</span><span class="o">.</span><span class="n">SOFTMAX</span><span class="o">.</span><span class="n">value</span>
        <span class="n">last_segment</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">(),</span> <span class="nb">id</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">n_classes_</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>
                                   <span class="n">RegressionModel</span><span class="o">=</span><span class="n">reg_model</span><span class="p">)</span>
        <span class="n">segments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">last_segment</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">segments</span></div>

<div class="viewcode-block" id="get_multiple_model_method"><span class="k">def</span> <span class="nf">get_multiple_model_method</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the type of multiple model method for MiningModels.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        Contains Xgboost model object</span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    The multiple model method for a MiningModel.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="s1">&#39;XGBClassifier&#39;</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">MULTIPLE_MODEL_METHOD</span><span class="o">.</span><span class="n">MODEL_CHAIN</span><span class="o">.</span><span class="n">value</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">MULTIPLE_MODEL_METHOD</span><span class="o">.</span><span class="n">SUM</span><span class="o">.</span><span class="n">value</span></div>

</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, maintainer@nyoka.org

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>